/**
 * Format price with dynamic decimal places
 * @param price - The price to format
 * @param showCurrency - Whether to show currency symbol (¥)
 * @returns Formatted price string
 */
export function formatPrice(price: string | number, showCurrency: boolean = true): string {
  const numPrice = typeof price === 'string' ? parseFloat(price) : price
  
  // Handle invalid numbers
  if (isNaN(numPrice) || numPrice < 0) {
    return showCurrency ? '¥0.00' : '0.00'
  }
  
  // If price is less than 0.01, show 4 decimal places
  if (numPrice < 0.01) {
    return showCurrency ? `¥${numPrice.toFixed(4)}` : numPrice.toFixed(4)
  }
  
  // Otherwise show 2 decimal places
  return showCurrency ? `¥${numPrice.toFixed(2)}` : numPrice.toFixed(2)
}

/**
 * Format price with conditional display based on user authentication
 * @param price - The price to format
 * @param isAuthenticated - Whether user is authenticated
 * @param showCurrency - Whether to show currency symbol (¥)
 * @returns Formatted price string or placeholder
 */
export function formatPriceAuth(
  price: string | number, 
  isAuthenticated: boolean, 
  showCurrency: boolean = true
): string {
  if (!isAuthenticated) {
    return showCurrency ? '¥0.00' : '0.00'
  }
  
  return formatPrice(price, showCurrency)
}

/**
 * 包装类型相关工具函数
 */

// 判断是否为中包装
export function isMidPack(midpack: number): boolean {
  return midpack > 1
}

// 判断是否支持拆零
export function supportsSplitting(midpack: number, isMidpackSaleDisabled: number): boolean {
  // 件包装默认支持拆零，中包装根据 isMidpackSaleDisabled 判断
  if (midpack <= 1) return true
  return isMidpackSaleDisabled === 0
}

// 获取包装类型显示文本
export function getPackingTypeText(midpack: number): string {
  return isMidPack(midpack) ? '中包装' : '件包装'
}

// 获取拆零支持状态文本
export function getSplittingSupportText(midpack: number, isMidpackSaleDisabled: number): string {
  if (!isMidPack(midpack)) return ''
  return supportsSplitting(midpack, isMidpackSaleDisabled) ? '支持拆零' : '不支持拆零'
}

// 获取最小购买数量
export function getMinOrderQuantity(midpack: number, isMidpackSaleDisabled: number): number {
  if (isMidPack(midpack) && !supportsSplitting(midpack, isMidpackSaleDisabled)) {
    return midpack
  }
  return 1
}

// 获取数量步长
export function getQuantityStep(midpack: number, isMidpackSaleDisabled: number): number {
  if (isMidPack(midpack) && !supportsSplitting(midpack, isMidpackSaleDisabled)) {
    return midpack
  }
  return 1
}

// 验证数量是否有效
export function isValidOrderQuantity(quantity: number, midpack: number, isMidpackSaleDisabled: number): boolean {
  if (isMidPack(midpack) && !supportsSplitting(midpack, isMidpackSaleDisabled)) {
    return quantity % midpack === 0
  }
  return quantity > 0
}

// 调整数量到最近的有效值
export function adjustToValidQuantity(quantity: number, midpack: number, isMidpackSaleDisabled: number): number {
  if (isMidPack(midpack) && !supportsSplitting(midpack, isMidpackSaleDisabled)) {
    return Math.max(midpack, Math.round(quantity / midpack) * midpack)
  }
  return Math.max(1, quantity)
}

// 获取包装信息说明文本
export function getPackingInfoText(midpack: number, unit: string, isMidpackSaleDisabled: number): string {
  if (isMidPack(midpack)) {
    const splittingText = getSplittingSupportText(midpack, isMidpackSaleDisabled)
    return `中包装: ${midpack}${unit}${splittingText ? ` (${splittingText})` : ''}`
  }
  return '件包装销售'
} 